﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using FortuneHeavenCommon.ComMethod;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;

namespace FortuneHeaven.Web.Lottery
{
    public partial class LotteryRegister : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.hidden);
            }
        }

        public void SubLottery(object sender, EventArgs e)
        {
            string username = this.txtAcount.Text.Trim();
            string password = this.txtPwd.Text.Trim();
            string validatecode = this.txtValidate.Text.Trim().ToLower();
            string lotterycode = this.txtLottery.Text.Trim();
            DateTime time = DateTime.Now;
            try
            {
                string error = "";
                AccountsInfo acount = AuthorityCenter.ValidateUserNoLogin(username, password, validatecode, ref error);
                if (acount!=null)
                {
                    if (string.IsNullOrEmpty(lotterycode))
                    {
                        ShowCurError("抽奖号码不能为空");
                    }
                    LotteryCodeBO lb = new LotteryCodeBO();
                    LotteryCode code = lb.GetLotteryCode(lotterycode);
                    if (code == null)
                    {
                        ShowCurError("抽奖号码不存在");
                        return;
                    }
                    //已抽奖
                    if (code.LotterysStatus == 2)
                    {
                        ShowCurError("抽奖号码已被使用");
                         return;
                    }
                    if (code.ValidDate < time)
                    {
                        ShowCurError("抽奖号码已过期");
                         return;
                    }
                    code.LotterysStatus = 2;
                    lb.Save(code);

                    LotteryOrderBO orderbo = new LotteryOrderBO();
                    LotteryOrder order = new LotteryOrder();
                    order.ID = orderbo.GetMaxID();
                    order.LotteryCode = code.Code;
                    order.LotteryID = code.ID;
                    order.LotteryType = code.LotteryType;
                    order.RequestTime = time;
                    order.Mail = this.txtMail.Text;
                    order.Mobile = this.txtMobile.Text;
                    order.QQ = this.txtQQ.Text;
                    order.UserID = acount.UserID;
                    order.UserName = acount.Accounts;
                    orderbo.Save(order, true);
                    Response.Redirect("LotterySuccess.aspx");
                }
                else
                {
                    ShowCurError(error);
                }

            }
            catch
            {
                ShowCurError("服务器异常");
            }
        }
    }
}